package com.bdqn.t350.user;

import com.bdqn.t350.dao.UserMapper;
import com.bdqn.t350.pojo.SysRole;
import com.bdqn.t350.pojo.User;
import com.bdqn.t350.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserTester {

    @Test
    public void countTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int count = sqlSession.getMapper(UserMapper.class).count();
        System.out.println(count);
    }

    @Test
    public void getUserListByPojoTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<User> list = sqlSession.getMapper(UserMapper.class).getUserListByPojo("admin", "系统管理员");
        System.out.println(list);
    }

    @Test
    public void getUserListByPojoMapTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        Map map = new HashMap<>();
        map.put("userCode", "admin");
        map.put("userName", "系统管理员");
        List<User> list = sqlSession.getMapper(UserMapper.class).getUserListByPojoMap(map);
        System.out.println(list);
    }

    @Test
    public void getUserListByUserCodeTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<User> list = sqlSession.getMapper(UserMapper.class).getUserListByUserCode("admin");
        System.out.println(list);
    }

    @Test
    public void selectUserAndAddressTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<User> list = sqlSession.getMapper(UserMapper.class).selectUserAndAddress("admin");
        System.out.println(list);
    }

    @Test
    public void addTest() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            User user = new User();
            user.setUserCode("t351");
            SysRole sysRole = new SysRole();
            sysRole.setId(1);
            sqlSession.getMapper(UserMapper.class).add(user,sysRole);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}
